package com.xiaoshuidi.cloud.module.finance.service.invoice.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.contract.api.ContractRentOrderApi;
import com.xiaoshuidi.cloud.module.contract.vo.*;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.*;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.invoice.BaseInvoiceMapper;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.invoice.FinanceInvoiceTitleConfigMapper;
import com.xiaoshuidi.cloud.module.finance.service.invoice.FinanceInvoiceService;
import com.xiaoshuidi.cloud.module.system.api.apartment.ApartmentManageApi;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import static com.xiaoshuidi.cloud.module.finance.enums.ErrorCodeConstants.FINANCE_INVOICE_RECORD_NOT_CONFIG;
import static com.xiaoshuidi.cloud.module.finance.enums.ErrorCodeConstants.FINANCE_INVOICE_RECORD_TIMEOUT;

@Service
public class FinanceInvoiceServiceImpl implements FinanceInvoiceService {

    @Resource
    private FinanceInvoiceTitleConfigMapper financeInvoiceTitleConfigMapper;
    @Resource
    private BaseInvoiceMapper baseInvoiceMapper;
    @Resource
    private ContractRentOrderApi contractRentOrderApi;


    @Override
    public PageResult<OrderInvoicePageRespVO> getInvoiceList(FinanceInvoicePageReqVO vo) {
        OrderInvoicePageReqVO reqDTO = new OrderInvoicePageReqVO();
        reqDTO.setPageNo(vo.getPageNo());
        reqDTO.setPageSize(vo.getPageSize());
        reqDTO.setApartmentName(vo.getApartmentName());
        reqDTO.setFullRoomName(vo.getFullRoomName());
        reqDTO.setRoommateName(vo.getRoommateName());
        reqDTO.setOrderNo(vo.getOrderNo());
        reqDTO.setRoommateId(vo.getRoommateId());
        reqDTO.setStatus(vo.getStatus());
        CommonResult<PageResult<OrderInvoicePageRespVO>> result = contractRentOrderApi.getInvoiceList(reqDTO);
        return result.getData();
    }

    @Override
    public List<BaseInvoiceExcelVO> getExportInvoiceList(FinanceInvoiceReqVO vo) {
        return baseInvoiceMapper.getExportInvoiceList(vo);
    }

    @Override
    public List<IssueInvoiceExcelVO> getExportIssueInvoiceList(IssueInvoiceReqVO vo) {
        return baseInvoiceMapper.getExportIssueInvoiceList(vo);
    }

    @Override
    public void changeInvoicesIssued(Long orderId) {
        contractRentOrderApi.updateOrderInvoiceStatus(orderId);
    }
}
